local skel = fk.CreateSkill {
  name = "lb__tianji",
  max_branches_use_time = {
    ["slash"] = {
      [Player.HistoryRound] = 1
    },
  }
}

Fk:loadTranslationTable {
  ["lb__tianji"] = "天击",
  [":lb__tianji"] = "每轮限一次，当你失去装备区的牌后，你可以弃置所有手牌并发动至多X次<a href=':lb__yueguan'><font color='red'>〖钺贯〗</font></a>（X为你装备区的牌数），且你每因此造成伤害后摸一张牌。",

  ["#lb__tianji"] = "天击：你可以弃置所有手牌并发动至多 %arg 次〖钺贯〗",

  ["$lb__tianji1"] = "我将，巡征追猎。",
  ["$lb__tianji2"] = "天击之责，至死方休。",
}

skel:addEffect(fk.AfterCardsMove, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if not (player:hasSkill("lb__tianji") and player:usedSkillTimes("lb__tianji", Player.HistoryRound, "slash") == 0
          and #player:getCardIds("e") > 0 and #player:getCardIds("h") > 0) then
      return
    end
    for _, move in ipairs(data) do
      return move.from and move.from == player and table.find(move.moveInfo, function(info)
        return info.fromArea == Card.PlayerEquip
      end)
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player,
      { skill_name = skel.name, prompt = "#lb__tianji:::" .. #player:getCardIds("e") })
  end,
  on_use = function(self, event, target, player, data)
    player:addSkillBranchUseHistory(skel.name, "slash")
    local room = player.room
    player:throwAllCards("h", self.name)
    for i = 1, #player:getCardIds("e"), 1 do
      if table.find(room:getAlivePlayers(), function(p)
            return not p:isKongcheng()
          end) then
        local t, use = room:askToUseActiveSkill(player, {
          skill_name = "lb__yueguan",
          prompt = "#lb__yueguan",
          cancelable = true,
          skip = true,
        })
        if t and use then
          local yueguan = Fk.skills["lb__yueguan"] ---@type ActiveSkill
          yueguan:onUse(room, SkillUseData:new {
            from = player,
            tos = use.targets,
            extra_data = {
              lb__tianji = true --需要手动传递extra_data
            },
          })
        else
          break
        end
      else
        break
      end
    end
    if player:getMark("@@lb__zaohuang") > 0 then
      player:setSkillUseHistory("lb__tianji")
    end
  end,
}, { check_skill_limit = true})

skel:addEffect(fk.Damage, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill("lb__tianji") and data.card and data.card.skillName == "lb__tianji"
  end,
  on_use = function(self, event, target, player, data)
    player:drawCards(1, "lb__tianji")
  end,
})

return skel
